<?php

/**
 * 后台文章栏目模块 Model
 * ============================================================================
 * TBlog TBlog博客系统　BY Tmac PHP MVC framework
 * $Author: zwt007 $  <zwttmac@qq.com>
 * $Id: category.class.php 99 2012-02-16 15:49:31Z zwt007 $
 * http://www.t-mac.org；
 */
class category extends Model
{

    private $db;
    private $category_table;

    /**
     * 初始化变量　定义私有变量
     */
    public function _init()
    {
        //连接数据库
        $this->db = $this->connect();
        $this->category_table = DB_PREFIX . 'category';
    }

    /**
     * 保存
     * @param string $menusMain
     * return Boole
     */
    public function addClass($info, $mode = 'INSERT', $where = '')
    {
        $rs = $this->db->autoExecute($this->category_table, $info, $mode, $where);
        return $rs;
    }

    /**
     * 获取一个资讯栏目信息
     * @param int $class_id 栏目id
     * return array
     */
    public function getCategoryInfo($class_id)
    {
        $rs = $this->db->getRow("SELECT * FROM $this->category_table WHERE cat_id = $class_id LIMIT 0,1");
        return $rs;
    }

    /**
     * del
     * @param int $class_id
     */
    public function delClass($cat_id)
    {
        $rs = $this->db->execute("DELETE FROM $this->category_table WHERE cat_id = $cat_id");
        return $rs;
    }

    /**
     * 取分类列表
     * @global type $category_list
     * @param type $parent
     * @param type $url
     * @param type $username
     * @param type $indexurl
     * @return type 
     */
    public function getCategoryList($parent)
    {
        $catetory_array = $this->getCategoryArray();
        global $category_list;
        $category_list = '';
        $category_list = $this->fenlei($catetory_array, $parent);
        return $category_list;
    }

    /**
     * 取出所有Category数组
     * @param type $uid
     * @return type 
     */
    public function getCategoryArray()
    {
        $sqltree = "SELECT cat_id,cat_pid,cat_name,channeltype,category_nicename,category_content FROM $this->category_table ORDER BY cat_order DESC, cat_id ASC";
        $rs = $this->db->getAll($sqltree);
        return $rs;
    }

    /**
     * 取无限分类
     * @global type $category_list
     * @param type $arr
     * @param type $parent
     * @param type $url
     * @param type $username
     * @param type $indexurl
     * @return string 
     */
    public function fenlei($arr, $parent)
    {
        global $category_list; //定义全局变量 返回值        
        $BASE_V = BASE_V . $GLOBALS['TmacConfig']['Template']['template_dir'] . '/';
        $num = count($arr);

        for ($i = 0; $i < $num; $i++) {//循环该层
            if ($i == 0) {
                for ($j = 0; $j < $num; $j++) {
                    if ($arr[$j]['cat_pid'] == $parent) {
                        $category_list .= '<ul class="category_list" style="padding:0px">';
                        break;
                    }//有元素的该层开始是输出<ul>，
                }
            }
            if ($arr[$i]['cat_pid'] == $parent) {  //层中符合父级id的元素输出                 
                $arr_array = $arr[$i];
                $category_content = Functions::cut_str(strip_tags($arr_array['category_content']), 42);
                $category_content = empty($category_content) ? '&nbsp;' : $category_content;
                $category_list .= "<li><dl>                
                    <dd class='cl_one'><div class='cate'><img src=\"{$BASE_V}images/desc.gif\" title='展开/收缩'><a href=\"" . PHP_SELF . "?m=archives.arclist&channelid=" . $arr_array['channeltype'] . "&cat_id=" . $arr_array['cat_id'] . "\">" . $arr_array['cat_name'] . "</a></div></dd>
                    <dd class='cl_two'>" . $arr_array['category_nicename'] . "</dd>
                    <dd class='cl_three'>$category_content</dd>                            
                    <dd class='cl_four'><a href=\"" . PHP_SELF . "?m=archives.catgoto&channelid=" . $arr_array['channeltype'] . "&cat_id=" . $arr_array['cat_id'] . "\">发布内容</a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href=\"" . PHP_SELF . "?m=archives.arclist&channelid=" . $arr_array['channeltype'] . "&cat_id=" . $arr_array['cat_id'] . "\">查看内容</a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href=\"" . PHP_SELF . "?m=category.add&cat_id=" . $arr_array['cat_id'] . "\">修改</a>&nbsp;&nbsp;|&nbsp;&nbsp;<a onclick=\"{if(confirm('删除将包括该分类的所有信息，确定删除吗?')){return true;}return false;}\"href=\"" . PHP_SELF . "?m=category.del&cat_id=" . $arr_array['cat_id'] . "\">删除</a></dd></dl>";
                $this->fenlei($arr, $arr[$i]['cat_id']);  //递归执行寻找当前元素的子元素，这些子元素<ul><li>当前元素<ul><li>子元素</li></ul></li></ul>
                $category_list .= "</li>";      //该</li>放在fenlei函数后是为了把形成该元素的子元素也要包含在该元素的<li>内
            }
            if ($i == ($num - 1)) {
                for ($j = 0; $j < $num; $j++) {
                    if ($arr[$j]['cat_pid'] == $parent) {
                        $category_list .= "</ul>";
                        break;
                    }//闭合ul
                }
            }
        }
        return $category_list;
    }

    /**
     * 取Select list
     * @global string $category_tree_list
     * @param type $parent
     * @param type $uid
     * @param type $current_id
     * @return string 
     */
    public function getCategoryTreeList($parent, $current_id)
    {
        $catetory_array = $this->getCategoryArray();
        global $category_tree_list;
        $category_tree_list = '';
        $category_tree_list = $this->tree_fenlei($catetory_array, $parent, 0, $current_id);
        return $category_tree_list;
    }

    /**
     * 递归取select option
     * @global string $category_tree_list
     * @param type $arr
     * @param type $parent
     * @param type $level
     * @param type $current_id
     * @return string 
     */
    public function tree_fenlei($arr, $parent, $level, $current_id)
    {
        global $category_tree_list; //定义全局变量 返回值        
        $BASE_V = BASE_V;
        $num = count($arr);
        $prefix_space = '';
        for ($j = 0; $j < $level; $j++) {
            $prefix_space .= "　"; //第一级就增加一个缩进            
        }

        for ($i = 0; $i < $num; $i++) {//循环该层
            if ($arr[$i]['cat_pid'] == $parent) {  //层中符合父级id的元素输出                 
                $arr_array = $arr[$i];
                if ((int) $arr_array['cat_id'] === (int) $current_id) {
                    $select = " selected";
                } else {
                    $select = "";
                }
                $category_tree_list .= '<option value="' . $arr_array['cat_id'] . '" ' . $select . '>' . $prefix_space . '|-' . $arr_array['cat_name'] . '</option>';
                $this->tree_fenlei($arr, $arr[$i]['cat_id'], $level + 1, $current_id);  //递归执行寻找当前元素的子元素，这些子元素<ul><li>当前元素<ul><li>子元素</li></ul></li></ul>                
            }
        }
        return $category_tree_list;
    }

    /**
     * 取所有的子ID
     * @global type $son_tree_list
     * @param type $parent
     * @return type 
     */
    public function getSonTreeList($parent)
    {
        $catetory_array = $this->getCategoryArray();
        global $son_tree_list;
        $son_tree_list = '';
        $son_tree_list = $this->son_fenlei($catetory_array, $parent);

        if (empty($son_tree_list)) {
            $cat_id_rs = $parent;
        } else {
            $cat_id_rs = $son_tree_list . ',' . $parent;
            $cat_id_rs = substr($cat_id_rs, 1);
        }
        return $cat_id_rs;
    }

    /**
     * 取所有的子ID
     * @global type $son_tree_list
     * @param type $arr
     * @param type $parent
     * @return type 
     */
    public function son_fenlei($arr, $parent)
    {
        global $son_tree_list; //定义全局变量 返回值        
        $num = count($arr);

        for ($i = 0; $i < $num; $i++) {//循环该层
            if ($arr[$i]['cat_pid'] == $parent) {  //层中符合父级id的元素输出                 
                $arr_array = $arr[$i];
                $son_tree_list .= ',' . $arr_array['cat_id'];
                $this->son_fenlei($arr, $arr[$i]['cat_id']);  //递归执行寻找当前元素的子元素，这些子元素<ul><li>当前元素<ul><li>子元素</li></ul></li></ul>                
            }
        }

        return $son_tree_list;
    }

    /**
     * 判断category_nicename唯一性的
     * @param type $category_nicename
     * @return type 
     */
    public function checkCategoryNicename($category_nicename)
    {
        $sql = "SELECT cat_id FROM $this->category_table WHERE category_nicename = '$category_nicename'";
        $cat_id = $this->db->getOne($sql);
        return $cat_id;
    }

}